<?php

namespace Common\Model;

/**
 * SettingModel.class.php实例模型
 * 
 * @author zhangzhen	
 * @date 2017-3-9
 */
class ShareCardRecordModel extends BaseModel {

    protected $trueTableName = 'tb_share_card_record';


    /**
     * 获取分享房卡记录
     * @modify zhangzhen 2017-3-10 创建
     * @param  string   $orderRoomId      
     * @return boolean 执行结果
     */
    public function getInfoByOrderRoomId($orderRoomId) {
        $option = [
            'orderroom_id' => $orderRoomId,
        ];
        $result = $this->alias('record')
                    ->field('admin.name as `by`,bookuser.name as bookuname,room.roomtype_name as roomtype,room.roominfo_name as roomname,record.areacode,record.mobile,record.createtime as time, opfrom')
                    ->join('tb_admin_login admin ON admin.id = record.creator','LEFT')
                    ->join('bk_user_login bookuser ON bookuser.id = record.creator','LEFT')
                    ->join('tb_order_room room ON room.id = record.orderroom_id','LEFT')
                    ->where($option)
                    ->order('record.id desc')
                    ->select();
        return empty($result) ? '' : $result;

    }

    /**
     * 获取分享房卡记录
     * @modify zhangzhen 2017-3-10 创建
     * @param  string   $orderRoomId      
     * @return boolean 执行结果
     */
    public function getPidByOrderRoomId($orderRoomId) {
        $option = [
            'orderroom_id' => $orderRoomId,
        ];
        $result = $this->field('potentialauthorityid')
                    ->where($option)
                    ->select();
        return empty($result) ? '' : $result;

    }

    /**
     * 增加分享房卡记录
     * @modify zhangzhen 2017-3-9 创建
     * @param  string   $orderRoomId      
     * @param  string   $mobile      手机
     * @return boolean 执行结果
     */
    public function getInfoByOrderRoomAndMobile($orderRoomId, $mobile, $areaCode = '86') {
        $option = [
        	'orderroom_id' => $orderRoomId,
            'mobile' => $mobile,
        	'areacode' => $areaCode,
            'isdelete' => 0,
        ];
        return $this->field('id,potentialauthorityid')->where($option)->find();
    }

    /**
     * 增加分享房卡记录
     * @modify zhangzhen 2017-3-9 创建
     * @param  string   $hotelId      客栈
     * @param  string   $orderRoomId      
     * @param  string   $potentialAuthorityId      `qrclient`.`tb_potential_authorith`.id
     * @param  string   $mobile      手机
     * @return boolean 执行结果
     */
    public function addInfo($hotelId, $orderRoomId, $potentialAuthorityId, $mobile, $areaCode = '86', $opfrom=0) {
        $data = [
        	'hotelentity_id' => $hotelId,
        	'orderroom_id' => $orderRoomId,
        	'potentialauthorityid' => $potentialAuthorityId,
            'mobile' => $mobile,
        	'areacode' => $areaCode,
        	'opfrom' => $opfrom,
        ];
        $data['createtime'] = date('Y-m-d H:i:s');

        return $this->add($data) === false ? false : true;
    }

    public function deleteInfo($id) {
        $option = array(
            'id' => $id,
            'isdelete' => NOT_DELETE,
        );
    
        $data = array(
            'isdelete' => IS_DELETE
        );
    
        $result = $this->where($option)->save($data);
        return !empty($result);
    }

}
